package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.adpter.RecreationRecyclerAdapter;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.VideoBean;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;

public class RecreationActivity extends CommonActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.recreation_rv_list)
    RecyclerView recreationRvList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    /**
     * 布局管理器
     */
    private LinearLayoutManager linearLayoutManager;

    /**
     * 视频列表对应的适配器
     */
    private RecreationRecyclerAdapter mAdapterVideo;
    private int pageNum = 1;
    private int pageSize = 5;

    SensorManager sensorManager;
    JZVideoPlayer.JZAutoFullscreenListener sensorEventListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.recreation_activity);
    }

    private void requestData() {
        showDialog(this,null);
        HttpParams params = new HttpParams();
        params.put("pageNum", pageNum++);
        params.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_RECREATION_VIDEO, params, Url.GET_RECREATION_VIDEO, new Callback<VideoBean>() {
            @Override
            public void onSuccess(VideoBean videoBean) {
                if (videoBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    if (pageNum - 1 == 1) {
                        if(videoBean.getMap().getPageInfo().getTotal()>0){
                            empty.setVisibility(View.GONE);
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                        mAdapterVideo.setNewData(videoBean.getMap().getPageInfo().getList());
                    } else {
                        mAdapterVideo.addData(videoBean.getMap().getPageInfo().getList());
                    }
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    mAdapterVideo.notifyDataSetChanged();

                    int pagecount;
                    if (videoBean.getMap().getPageInfo().getTotal() % pageSize == 0) {
                        pagecount = videoBean.getMap().getPageInfo().getTotal() / pageSize;
                    } else {
                        pagecount = videoBean.getMap().getPageInfo().getTotal() / pageSize + 1;
                    }

                    if (pageNum > pagecount) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.setNoMoreData(false);
                    }
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    showToast(videoBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void initView() {
        setTitle("娱乐");
        refreshLayout.setOnRefreshLoadMoreListener(this);
        linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);//布局
        recreationRvList.setLayoutManager(linearLayoutManager);
        recreationRvList.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.HORIZONTAL));//设置分割线

        mAdapterVideo = new RecreationRecyclerAdapter(R.layout.recreation_list_video_item, this);
        recreationRvList.setAdapter(mAdapterVideo);
        recreationRvList.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {
                JZVideoPlayer.onChildViewAttachedToWindow(view, R.id.video);
            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                JZVideoPlayer.onChildViewDetachedFromWindow(view);
            }
        });

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JZVideoPlayer.JZAutoFullscreenListener();
    }

    @Override
    protected void initData() {
        pageNum = 1;
        requestData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        requestData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        requestData();
    }
}
